package api

import (
	"FeedXmh/app/dao"
	jwt "github.com/gogf/gf-jwt"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

var Action = action{}

type action struct {}
func (* action)GET(r *ghttp.Request) {
	where := "1=1 "
	params := r.GetQueryMap()
	key :=gconv.String(params["key"])
	if key != "" {
		where += " and concat(id,name,name_en) like '%" + key + "%' "
	}

	id :=gconv.String(params["id"])
	if id != "" {
		where += " and id='"+id+"'"
	}
	claims := jwt.ExtractClaims(r)
	userid := claims["userid"].(string)
	url := r.URL.Path
	db:=g.DB(g.Cfg().GetString("custom.db_name"))
	if dao.CheckApiPermission(url,r.Method, userid) {
		model := db.Model("t_action")
		model = model.Where(where).OrderBy(" `order` asc")
		res, err := model.Select()
		if err != nil {
			g.Log().Line().Error(err)
		}
		r.Response.WriteJson(g.Map{
			"code":  0,
			"msg":   "",
			"count": len(res),
			"data":  res.List(),
		})
	} else {
		r.Response.WriteJson(g.Map{
			"code":  0,
			"msg":   "permission denied",
			"count": 0,
			"data":  nil,
		})
	}
	r.ExitAll()
}

func (* action)PUT(r *ghttp.Request) {
	res := g.Map{
		"code": -1,
		"msg":  "",
	}
	claims := jwt.ExtractClaims(r)
	userid := claims["userid"].(string)
	url := r.URL.Path
	params := r.GetFormMap()
	db:=g.DB(g.Cfg().GetString("custom.db_name"))

	if dao.CheckApiPermission(url,r.Method, userid) {
		if tx, err := db.Begin(); err == nil {
			_, err := tx.Model("t_action").Data(params).Where("id=?", params["id"]).Update()
			if err != nil {
				res["code"]=-1
				res["msg"]=err.Error()
				tx.Rollback()

			}else{
				res["code"]=0
				tx.Commit()
			}
		}else{
			res["code"]=-1
			res["msg"]=err.Error()
		}
	} else {
		res["code"] = -1
		res["msg"] = "Permission denied"
	}
	r.Response.WriteJson(res)
	r.ExitAll()
}

func (* action)POST(r *ghttp.Request) {
	res := g.Map{
		"code": -1,
		"msg":  "",
	}
	claims := jwt.ExtractClaims(r)
	userid := claims["userid"].(string)
	url := r.URL.Path
	params := r.GetFormMap()
	db:=g.DB(g.Cfg().GetString("custom.db_name"))
	if dao.CheckApiPermission(url,r.Method, userid) {
		if tx, err := db.Begin(); err == nil {
			_, err := tx.Model("t_action").Data(params).Insert()
			if err != nil {
				res["code"]=-1
				res["msg"]=err.Error()
				tx.Rollback()

			}else{
				res["code"]=0
				tx.Commit()
			}
		}else{
			res["code"]=-1
			res["msg"]=err.Error()
		}
	} else {
		res["code"] = -1
		res["msg"] = "Permission denied"
	}
	r.Response.WriteJson(res)
	r.ExitAll()
}

func (* action)DELETE(r *ghttp.Request) {
	res := g.Map{
		"code": 0,
		"msg":  "",
	}
	id := r.GetFormMap()["id"]
	claims := jwt.ExtractClaims(r)
	current_user := claims["userid"].(string)
	url := r.URL.Path
	db:=g.DB(g.Cfg().GetString("custom.db_name"))
	if dao.CheckApiPermission(url,r.Method, current_user) {
		sqlres, _ := db.Model("t_action").Where("id=?", id).Delete()
		if a, err := sqlres.RowsAffected(); err == nil && a >= 0 {
			res["code"] = 0
			res["msg"] = "保存成功"
		} else {
			res["code"] = -1
			res["msg"] = "保存失败"
		}
	} else {
		res["code"] = -1
		res["msg"] = "Permission denied"
	}
	r.Response.WriteJson(res)
	r.ExitAll()
}
